package com.sunny.setavatordemo.dialog;


import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.sunny.setavatordemo.R;


public class UploadConfirmDialog extends DialogFragment {

    private static final String PARAM_PATH = "path";


    private String path;
    private OnDialogInteractionListener interactionListener;

    public UploadConfirmDialog() {
        // Required empty public constructor
    }

    public static UploadConfirmDialog newInstance(String path) {

        UploadConfirmDialog dialog = new UploadConfirmDialog();

        Bundle bundle = new Bundle();
        bundle.putString(PARAM_PATH, path);
        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {

            path = getArguments().getString(PARAM_PATH);
        }
    }

    public void addOnDialogInteractionListener(OnDialogInteractionListener listener) {

        this.interactionListener = listener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.confirm_dialog);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
//        lp.alpha=0.3f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        dialog.findViewById(R.id.upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //上传图片

                interactionListener.onDialogInteraction(0, path);
                dismiss();
            }
        });
        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                interactionListener.onDialogInteraction(1, null);
                dismiss();
            }
        });

        return dialog;
    }

    public interface OnDialogInteractionListener {

        void onDialogInteraction(int action, String path);
    }
}
